<?php
require_once 'playerleaguematches.php';

class PlayerMatches {
  const SENIORLEAGUE_XPATH = "./senior_league[1]";
  const INTERNATIONALCUP_XPATH = "./international_cup[1]";
  const LEAGUECUP_XPATH = "./league_cup[1]";
  const SHADOWCUP_XPATH = "./shadow_cup[1]";
  const YOUTHWORLDCUP_XPATH = "./youth_world_cup[1]";
  const YOUTHLEAGUE_XPATH = "./youth_league[1]";

  private $db;
  private $seniorLeague;
  private $internationalCup;
  private $leagueCup;
  private $shadowCup;
  private $youthWorldCup;
  private $youthLeague;

  public function __construct($database) {
    $this->db = $database;

    $this->internationalCup = new PlayerLeagueMatches($this->db);
    $this->leagueCup = new PlayerLeagueMatches($this->db);
    $this->seniorLeague = new PlayerLeagueMatches($this->db);
    $this->shadowCup = new PlayerLeagueMatches($this->db);
    $this->youthWorldCup = new PlayerLeagueMatches($this->db);
    $this->youthLeague = new PlayerLeagueMatches($this->db);
  }

  public function loadFromNode(DOMElement $docNode) {
    $doc = $docNode->ownerDocument;

    $xPath = new DOMXPath($doc);

    $qResult = $xPath->query(PlayerMatches::INTERNATIONALCUP_XPATH,$docNode);
    if ($qResult->length!=0) {
      $this->internationalCup->loadFromNode($qResult->item(0));
    }
    $qResult = $xPath->query(PlayerMatches::LEAGUECUP_XPATH,$docNode);
    if ($qResult->length!=0) {
      $this->leagueCup->loadFromNode($qResult->item(0));
    }
    $qResult = $xPath->query(PlayerMatches::SENIORLEAGUE_XPATH,$docNode);
    if ($qResult->length!=0) {
      $this->seniorLeague->loadFromNode($qResult->item(0));
    }
    $qResult = $xPath->query(PlayerMatches::SHADOWCUP_XPATH,$docNode);
    if ($qResult->length!=0) {
      $this->shadowCup->loadFromNode($qResult->item(0));
    }
    $qResult = $xPath->query(PlayerMatches::YOUTHWORLDCUP_XPATH,$docNode);
    if ($qResult->length!=0) {
      $this->youthWorldCup->loadFromNode($qResult->item(0));
    }
    $qResult = $xPath->query(PlayerMatches::YOUTHLEAGUE_XPATH,$docNode);
    if ($qResult->length!=0) {
      $this->youthLeague->loadFromNode($qResult->item(0));
    }
  }

  public function toXHTML() {
    $xhtml = "<table border=\"1\">";
    $xhtml .= "<tr><th/><th colspan=4>This</th><th colspan=4>Last</th><th colspan=4>Career</th></tr>";
    $xhtml .= "<tr><td/><th>M</th><th>S</th><th>A</th><th>D</th><th>M</th><th>G</th><th>A</th><th>D</th><th>M</th><th>G</th><th>A</th><th>D</th></tr>";
    if ($this->seniorLeague) {
      $xhtml .= "<tr><th>Senior League</th>{$this->seniorLeague->toXHTML()}</tr>";
    }
    if ($this->internationalCup) {
      $xhtml .= "<tr><th>International Cup</th>{$this->internationalCup->toXHTML()}</tr>";
    }
    if ($this->leagueCup) {
      $xhtml .= "<tr><th>League Cup</th>{$this->leagueCup->toXHTML()}</tr>";
    }
    if ($this->shadowCup) {
      $xhtml .= "<tr><th>Shadow Cup</th>{$this->shadowCup->toXHTML()}</tr>";
    }
    if ($this->youthWorldCup) {
      $xhtml .= "<tr><th>Youth World Cup</th>{$this->youthWorldCup->toXHTML()}</tr>";
    }
    if ($this->youthLeague) {
      $xhtml .= "<tr><th>Youth League</th>{$this->youthLeague->toXHTML()}</tr>";
    }

    $xhtml .= "</table>";
    return $xhtml;
  }

  public function insertNewToDB($playerId) {
    $query = $this->db->query('SHOW TABLE STATUS LIKE \'playermatches\';');
    if (!$query) {
      log_message('error', 'Database is inconsistance!');
    }

    $matchesId = $query->row()->Auto_increment;

    $seniorLeagueId = NULL;
    if ($this->seniorLeague != NULL) {
      $seniorLeagueId = $this->seniorLeague->insertNewToDB($matchesId);
    }

    $internationalCupId = NULL;
    if ($this->internationalCup!=NULL) {
      $internationalCupId = $this->internationalCup->insertNewToDB($matchesId);
    }

    $leagueCupId = null;
    if ($this->leagueCup!=NULL) {
      $leagueCupId = $this->leagueCup->insertNewToDB($matchesId);
    }

    $shadowCupId = null;
    if ($this->shadowCup!=NULL) {
      $shadowCupId = $this->shadowCup->insertNewToDB($matchesId);
    }

    $youthWorldCupId = null;
    if ($this->youthWorldCup!=NULL) {
      $youthWorldCupId = $this->youthWorldCup->insertNewToDB($matchesId);
    }

    $youthLeagueId = null;
    if ($this->youthLeague!=NULL) {
      $youthLeagueId = $this->youthLeague->insertNewToDB($matchesId);
    }


    $this->db->set('id',$matchesId);
    $this->db->set('playerId',$playerId);
    $this->db->set('seniorLeague',$seniorLeagueId);
    $this->db->set('internationalCup',$internationalCupId);
    $this->db->set('leagueCup',$leagueCupId);
    $this->db->set('shadowCup',$shadowCupId);
    $this->db->set('youthWorldCup',$youthWorldCupId);
    $this->db->set('youthLeague',$youthLeagueId);
    $query = $this->db->insert('playermatches');

    if (!$query) {
      log_message('error', 'Error during matches inserttion!');
    }
    return $matchesId;
  }

  public function amendOldInDB($matchesId) {
    $this->db->where('id',$matchesId);
    $query = $this->db->get('playermatches');

    if (!$query) {
      log_message('error','Error during playermatches amendment!');
      return FALSE;
    }
    $tMatches = $query->row();

    $seniorLeague = $this->seniorLeague->amendOldInDB($tMatches->seniorLeague);
    $internationalCup = $this->internationalCup->amendOldInDB($tMatches->internationalCup);
    $leagueCup = $this->leagueCup->amendOldInDB($tMatches->leagueCup);
    $shadowCup = $this->shadowCup->amendOldInDB($tMatches->shadowCup);
    $youthWorldCup = $this->youthWorldCup->amendOldInDB($tMatches->youthWorldCup);
    $youthLeague = $this->youthLeague->amendOldInDB($tMatches->youthLeague);

    $this->db->set('seniorLeague',$seniorLeague);
    $this->db->set('internationalCup',$internationalCup);
    $this->db->set('leagueCup',$leagueCup);
    $this->db->set('shadowCup',$internationalCup);
    $this->db->set('youthWorldCup',$youthWorldCup);
    $this->db->set('youthLeague',$youthLeague);
    $this->db->where('id',$matchesId);
    $query = $this->db->update('playermatches');

    if (!$query) {
      log_message('error', 'Error during playermatches amendment!');
    }

    return $matchesId;
  }

  public function loadFromDB($matchesId) {
    $this->db->where('id',$matchesId);
    $query = $this->db->get('playermatches');

    $stmt = $conn->Prepare("SELECT * FROM `playermatches` WHERE `matchesId` = ?");
    $rs=$conn->Execute($stmt,array($matchesId));

    if (!$query || $query->num_rows()==0) {
      log_message('error', 'PlayerAge ('.$ageId.') not found in the database!');
    } else {
      $result = $query->row();

      if ($result['seniorLeague']!=NULL) {
        $this->seniorLeague->loadFromDB($conn,$result['seniorLeague']);
      }

      if ($result['internationalCup']!=NULL) {
        $this->internationalCup->loadFromDB($conn,$result['internationalCup']);
      }

      if ($result['leagueCup']!=NULL) {
        $this->leagueCup->loadFromDB($conn,$result['leagueCup']);
      }

      if ($result['shadowCup']!=NULL) {
        $this->shadowCup->loadFromDB($conn,$result['shadowCup']);
      }

      if ($result['youthWorldCup']!=NULL) {
        $this->youthWorldCup->loadFromDB($conn,$result['youthWorldCup']);
      }

      if ($result['youthLeague']!=NULL) {
        $this->youthLeague->loadFromDB($conn,$result['youthLeague']);
      }
    }
  }
}
?>
